﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace syc.ZhiJian
{
    public partial class 产品台账 : System.Web.UI.Page
    {
        private string sql = @"SELECT     ID, 炉号, 捆号, 重量, 长度, 支数, 入库日期, 出库日期
                               FROM   dbo.产品库存";
        protected void Page_Load(object sender, EventArgs e)
        {

            this.Message.Text = "·准备就绪";
            if (!this.IsPostBack)
            {
                sql = sql + " where [产品类别ID]=" + Request.QueryString["id"];
                this.SqlDataSource1.SelectCommand = sql;
                this.ViewState["SelectCommand"] = sql;
            }
            this.SqlDataSource1.SelectCommand = (string)this.ViewState["SelectCommand"];
        }

        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }

        }
        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);
        }
        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            }

        }

        protected void Button_Query_Click(object sender, EventArgs e)
        {
            string temp = this.DanHao.Text.Trim().Replace("'", "");
            string querySql = "";
            if (temp != "")
            {
                querySql = "[捆号] like '%" + temp + "'";
            }
            DateTime Dt;
            if (DateTime.TryParse(this.Date1.Text, out Dt))
            {
                if (querySql != "")
                {
                    querySql += " and ";
                }
                querySql += "[入库日期]>='" + Dt.ToShortDateString() + "'";
            }
            if (DateTime.TryParse(this.Date2.Text, out Dt))
            {
                if (querySql != "")
                {
                    querySql += " and ";
                }
                querySql += "[入库日期]<='" + Dt.ToShortDateString() + "'";
            }
            if (querySql == "")
            {
                this.SqlDataSource1.SelectCommand = @"SELECT     ID, 炉号, 捆号, 重量, 长度, 支数, 入库日期, 出库日期
                                                     FROM         dbo.产品库存
                                        where  [产品库存].[产品类别ID]=" + Request.QueryString["id"];
            }
            else
            {
                this.SqlDataSource1.SelectCommand = @"SELECT     ID, 炉号, 捆号, 重量, 长度, 支数, 入库日期, 出库日期
                                                     FROM         dbo.产品库存
                                        where  [产品库存].[产品类别ID]=" + Request.QueryString["id"] + querySql;
            }
            this.ViewState["SelectCommand"] = this.SqlDataSource1.SelectCommand;
        }

        //查看
        protected void Button_Edit_Click(object sender, EventArgs e)
        {
            if (GridView1.SelectedIndex >= 0 && GridView1.Rows.Count > 0)
            {
                Response.Redirect("ChanPin_Out.aspx?ID=" + GridView1.DataKeys[GridView1.SelectedIndex].Values[0].ToString());
            }
            else
            {
                this.Message.Text = "·请先选择一个项目";
            }
        }

        //保存结果 
        protected void Button_Save_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                CheckBox cb = (CheckBox)GridView1.Rows[i].Cells[12].Controls[1];
                string ID = this.GridView1.Rows[i].Cells[1].Text;
                if (cb.Checked == true)
                {
                    this.SqlDataSource1.UpdateCommand = "Exec RuKuTongZhi_ZhiJian_Update " + "true," + ID;
                    this.SqlDataSource1.UpdateCommandType = SqlDataSourceCommandType.Text;
                    this.SqlDataSource1.Update();
                    this.GridView1.DataSourceID = this.SqlDataSource1.ID;
                    this.Message.Text = "·质检信息保存成功";
                }
                else if (cb.Checked == false)
                {
                    this.SqlDataSource1.UpdateCommand = "Exec RuKuTongZhi_ZhiJian_Update " + "false," + ID;
                    this.SqlDataSource1.UpdateCommandType = SqlDataSourceCommandType.Text;
                    this.SqlDataSource1.Update();
                    this.GridView1.DataSourceID = this.SqlDataSource1.ID;
                    this.Message.Text = "·质检信息保存成功";
                }
            }
        }

    }
}
